package com.wanczy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanczy.pojo.MenuTable;
import com.wanczy.service.MenuOpService;
import com.wanczy.service.impl.MenuOpServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet("/servlet/MenuQueryByUserIdServlet")
public class MenuQueryByUserIdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();// ȡ��out����
		
		int userId = Integer.parseInt(request.getParameter("userId"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int pageCurrent = Integer.parseInt(request.getParameter("pageCurrent"));
		String menuName = request.getParameter("menuName");
		MenuOpService service = new MenuOpServiceImpl();
		int count = service.findCountByMenuName(userId);
		List<MenuTable> list = service.display(userId, pageSize, pageCurrent, menuName);
		JSONArray jsonA = new JSONArray();
		JSONObject jsonO = new JSONObject();
		jsonO.put("count", count);
		jsonA.add(jsonO);
		for (MenuTable menuTable : list) {
			JSONObject json = new JSONObject();
			json.put("menuId", menuTable.getMenu_id());
			json.put("menuName", menuTable.getMenu_name());
			json.put("menuDescribe", menuTable.getMenu_describe());
			if(menuTable.getMenu_state() == 1) {
				json.put("menuState", "可用");
			}else {
				json.put("menuState", "停用");
			}
			json.put("menuUrl", menuTable.getMenu_url());
			if(menuTable.getMenu_level().length()==3) {
				json.put("menuLevel", "一级菜单");
			}else {
				json.put("menuLevel", "二级菜单");
			}
			jsonA.add(json);
		}
		out.println(jsonA.toString());
		out.close();
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
